package com.zzyl.nursing.controller.member;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.CountByTimeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询取消预约数量
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        return R.ok(reservationService.getCancelledReservationCount());
    }


    /**
     * 分页查询预约信息列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取预约信息列表")
    public R<TableDataInfo<List<Reservation>>> list(Reservation memberReservation) {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(memberReservation);
        return R.ok(getDataTable(list));
    }

    /**
     * 导出预约信息列表
     */
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出预约信息列表")
    public void export(HttpServletResponse response, Reservation reservation) {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取预约信息详细信息")
    public R<Reservation> getInfo(@ApiParam(value = "预约信息ID", required = true) @PathVariable("id") Long id) {
        return R.ok(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation) {
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 删除预约信息
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除预约信息")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids) {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }

    /**
     * 获取预约时间段内预约数量
     * @return
     */
    @GetMapping("/countByTime")
    public R<List<CountByTimeVo>> getCountByTime(@RequestParam long time){
        return R.ok(reservationService.getCountByTime(time));
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult cancelReservation(@PathVariable Long id) {
        return toAjax(reservationService.cancelReservation(id));
    }
}